package com.jyxd.web.controller.dictionary;

import cn.hutool.core.date.DateUtil;
import com.jyxd.web.data.dictionary.MonitorDictionary;
import com.jyxd.web.data.user.User;
import com.jyxd.web.service.dictionary.MonitorDictionaryService;
import com.jyxd.web.util.HttpCode;
import com.jyxd.web.util.JsonArrayValueProcessor;
import com.jyxd.web.util.UUIDUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@RequestMapping(value = "/monitorDictionary")
public class MonitorDictionaryController {

    private static Logger logger = LoggerFactory.getLogger(MonitorDictionaryController.class);

    @Autowired
    private MonitorDictionaryService monitorDictionaryService;

    /**
     * 新增一条监护仪字典表数据
     *
     * @param monitorDictionary
     * @return
     */
    @RequestMapping(value = "/insert")
    @ResponseBody
    public String insert(@RequestBody MonitorDictionary monitorDictionary,HttpSession session) {
        User user=(User)session.getAttribute("user");
        JSONObject json = new JSONObject();
        monitorDictionary.setId(UUIDUtil.getUUID());
        try {
            monitorDictionary.setTenantId(user.getTenantId());
            monitorDictionaryService.insert(monitorDictionary);
            json.put("code", HttpCode.OK_CODE.getCode());
            json.put("msg", "添加成功");
        } catch (Exception e) {
            json.put("code", HttpCode.FAILURE_CODE.getCode());
            json.put("msg", "添加失败");
        }
        return json.toString();
    }

    /**
     * 更新一条监护仪字典表数据
     * queryList
     *
     * @param map
     * @return
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public String update(@RequestBody(required = false) Map<String, Object> map) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("msg", "更新失败");
        if (map != null && map.containsKey("id") && map.containsKey("status")) {
            MonitorDictionary monitorDictionary = monitorDictionaryService.queryData(map.get("id").toString());
            if (monitorDictionary != null) {
                monitorDictionary.setStatus((int) map.get("status"));
                monitorDictionaryService.update(monitorDictionary);
                json.put("msg", "更新成功");
                json.put("code", HttpCode.OK_CODE.getCode());
            } else {
                json.put("msg", "更新失败，查无此条数据。");
                return json.toString();
            }
        }
        return json.toString();
    }

    /**
     * 编辑监护仪字典表数据
     *
     * @param map
     * @return
     */
    @RequestMapping(value = "/edit")
    @ResponseBody
    public String edit(@RequestBody(required = false) Map<String, Object> map) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("msg", "编辑失败");
        if (map != null && map.containsKey("id") && map.containsKey("status") && map.containsKey("monitorName") && map.containsKey("monitorIp") && map.containsKey("monitorPort")) {
            MonitorDictionary monitorDictionary = monitorDictionaryService.queryData(map.get("id").toString());
            if (monitorDictionary != null) {
                monitorDictionary.setStatus((int) map.get("status"));
                monitorDictionary.setMonitorName(map.get("monitorName").toString());
                monitorDictionary.setMonitorIp(map.get("monitorIp").toString());
                monitorDictionary.setMonitorPort(map.get("monitorPort").toString());
                monitorDictionary.setMonitorType(map.get("monitorType").toString());
                monitorDictionary.setDeviceRole(map.get("deviceRole").toString());
                monitorDictionary.setMonitorCode(map.get("monitorCode").toString());
                monitorDictionary.setConnectType(map.get("connectType").toString());
                monitorDictionary.setMonitorSeriesNumber(map.get("monitorSeriesNumber").toString());
                monitorDictionary.setManufacturersName(map.get("manufacturersName").toString());
                monitorDictionary.setTypeCode(Integer.parseInt(map.get("typeCode").toString()));
                monitorDictionary.setOfficeCode((String) map.get("officeCode"));
                monitorDictionary.setOfficeName((String) map.get("officeName"));
                if(map.containsKey("procurementDate") && StringUtils.isNotEmpty(map.get("procurementDate").toString())){
                    monitorDictionary.setProcurementDate(DateUtil.parseDate(map.get("procurementDate").toString()));
                }
                if (Objects.nonNull(map.get("manufacturersId"))) {
                    monitorDictionary.setManufacturersId(map.get("manufacturersId").toString());
                }
                monitorDictionaryService.update(monitorDictionary);
                json.put("msg", "编辑成功");
                json.put("code", HttpCode.OK_CODE.getCode());
            } else {
                json.put("msg", "编辑失败，查无此条数据");
                return json.toString();
            }
        }
        return json.toString();
    }

    /**
     * 删除一条监护仪字典表数据
     *
     * @param map
     * @return
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public String delete(@RequestBody(required = false) Map<String, Object> map) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("msg", "删除失败");
        if (map.containsKey("id")) {
            MonitorDictionary monitorDictionary = monitorDictionaryService.queryData(map.get("id").toString());
            if (monitorDictionary != null) {
                monitorDictionary.setStatus(-1);
                monitorDictionaryService.update(monitorDictionary);
                json.put("msg", "删除成功");
                json.put("code", HttpCode.OK_CODE.getCode());
            } else {
                json.put("msg", "删除失败,查无此条数据");
                return json.toString();
            }
        }
        return json.toString();
    }

    /**
     * 根据主键id查询一条监护仪数据
     *
     * @param map
     * @return
     */
    @RequestMapping(value = "/queryData", method = RequestMethod.POST)
    @ResponseBody
    public String queryData(@RequestBody(required = false) Map<String, Object> map) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        json.put("msg", "暂无数据");
        if (map != null && map.containsKey("id")) {
            MonitorDictionary monitorDictionary = monitorDictionaryService.queryData(map.get("id").toString());
            if (monitorDictionary != null) {
                json.put("msg", "查询成功");
                json.put("data", JSONObject.fromObject(monitorDictionary));
            }
        }
        json.put("code", HttpCode.OK_CODE.getCode());
        return json.toString();
    }

    /**
     * 根据条件查询监护仪数据列表（可分页）
     *
     * @param map
     * @return
     */
    @RequestMapping(value = "/queryList", method = RequestMethod.POST)
    @ResponseBody
    public String queryList(@RequestBody(required = false) Map<String, Object> map,HttpSession session) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        json.put("msg", "暂无数据");
        if (map == null) map = new HashMap<>();
        User user=(User)session.getAttribute("user");
        if(user!=null && StringUtils.isNotEmpty(user.getTenantId())){
            String tenantId = user.getTenantId();
            if(StringUtils.isNotEmpty(tenantId)){
                map.put("tenantId",tenantId);
            }
        }

        if (map.containsKey("start")) {
            int totalCount = monitorDictionaryService.queryNum(map);
            map.put("start", ((int) map.get("start") - 1) * (int) map.get("size"));
            json.put("totalCount", totalCount);
        }
        List<MonitorDictionary> list = monitorDictionaryService.queryList(map);
        if (list != null && list.size() > 0) {
            JsonConfig jsonConfig = new JsonConfig();
            jsonConfig.registerJsonValueProcessor(Date.class, new JsonArrayValueProcessor());
            json.put("msg", "查询成功");
            json.put("data", JSONArray.fromObject(list,jsonConfig));
        }
        json.put("code", HttpCode.OK_CODE.getCode());
        return json.toString();
    }

    /**
     * 床位安排--查询未分配的监护仪
     *
     * @param map
     * @return
     */
    @RequestMapping(value = "/getNoBedMonitorList", method = RequestMethod.POST)
    @ResponseBody
    public String getNoBedMonitorList(@RequestBody(required = false) Map<String, Object> map, HttpSession session) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        json.put("msg", "暂无数据");
        if (map == null) map = new HashMap<>();
        User user=(User)session.getAttribute("user");
        if(user!=null && StringUtils.isNotEmpty(user.getTenantId())){
            String tenantId = user.getTenantId();
            if(StringUtils.isNotEmpty(tenantId)){
                map.put("tenantId",tenantId);
            }
        }

        List<MonitorDictionary> list = monitorDictionaryService.getNoBedMonitorList(map);
        if (list != null && list.size() > 0) {
            json.put("msg", "查询成功");
            json.put("data", JSONArray.fromObject(list));
        }
        json.put("code", HttpCode.OK_CODE.getCode());
        return json.toString();
    }

}
